package com.server.service;

import com.server.Untils.MybatisClass;
import com.server.Untils.ResponseMessage;
import com.server.entity.Books;
import com.server.example.BooksExample;
import com.server.mapper.BooksMapper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author Admin
 */
public class BooksService {

    /**
     * 得到上架的图书信息列表
     * <br/>
     * 调用方法： List<Books> usernamesLogin = new BooksService().getList()
     * @param booksName 当 booksName 为空时候返回记录信息/当booksName不为空返回like的信息记录
     * @param isShow 返回是否上架的图书信息 true: 上架的图书信息/false: 下架的图书信息
     * @return 得到上架的图书信息列表
     */
    public List<Books> getList(Boolean isShow,String booksName){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        BooksMapper booksMapper = sqlSession.getMapper(BooksMapper.class);
        BooksExample booksExample = new BooksExample();
        if ("".equals(booksName)) {
            booksExample.createCriteria().andShowsEqualTo(isShow);
        }else{
            booksExample.createCriteria().andShowsEqualTo(isShow).andBooknameLike(booksName);
        }
        List<Books> booksList= booksMapper.selectByExample(booksExample);
        sqlSession.close();
        return booksList;
    }

    /**
     * 得到上架的图书信息列表
     * <br/>
     * 调用方法： List<Books> usernamesLogin = new BooksService().getList()
     * @param booksName 当 booksName 为空时候返回记录信息/当booksName不为空返回like的信息记录
     * @return 得到上架的图书信息列表
     */
    public List<Books> getList(String booksName){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        BooksMapper booksMapper = sqlSession.getMapper(BooksMapper.class);
        BooksExample booksExample = new BooksExample();
        if ("".equals(booksName)) {
            booksExample.createCriteria().andBidIsNotNull();
        }else{
            booksExample.createCriteria().andBidIsNotNull().andBooknameLike(booksName);
        }
        List<Books> booksList= booksMapper.selectByExample(booksExample);
        sqlSession.close();
        return booksList;
    }

    /**
     * 选购图书的功能实现
     * <br/>
     * 调用方法：Books usernamesLogin = new BooksService().selectByPrimaryKey(27);
     *
     * @param id the id
     * @return the books
     */
    public Books selectByPrimaryKey(Integer id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        Books types=null;
        BooksMapper booksMapper = sqlSession.getMapper(BooksMapper.class);
        try {
            types=booksMapper.selectByPrimaryKey(id);
        }catch (Exception e) {
            /*没有找到数据*/
            types=new Books();
        }finally {
            sqlSession.close();
        }
        return types;
    }

    /**
     * 修改图书的功能实现/下架图书的功能测试
     * <br/>
     * - 修改图书调用方法 修改数据表的相关字段值
     * - 下架图书的功能测试 修改数据表的 shows 字段 ==> true => false
     * <br/>
     * 调用方法：String usernamesLogin = new BooksService().updateByPrimaryKeySelective(books);
     * - 修改成功 The request is successful
     * - 修改失败/图书信息不存在 "No record related information found"
     * @param books
     * @return
     */
    public String updateByPrimaryKeySelective(Books books){
        String returnString="";
        SqlSession sqlSession = MybatisClass.getSqlSession();
        BooksMapper booksMapper = sqlSession.getMapper(BooksMapper.class);
        try {
            int types=booksMapper.updateByPrimaryKeySelective(books);
            returnString =types>=1?ResponseMessage.LOGIN_SUCCESS:ResponseMessage.NOT_FOUND_ERROR;
        }catch (Exception e) {
            returnString= ResponseMessage.SERVER_ERROR;
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
        return returnString;
    }

    /**
     * 添加图书功能实现
     * <br/>
     * 调用方法：String usernamesLogin = new BooksService().insertBooks(books);
     * - 成功 The request is successful
     * - 失败 Other unknown errors,Please contact the developer
     * <br/>
     * @param books 要新建的图书信息
     * @return 返回是否成功
     */
    public String insertBooks(Books books) {
        String returnString="";
        SqlSession sqlSession = MybatisClass.getSqlSession();
        BooksMapper booksMapper = sqlSession.getMapper(BooksMapper.class);
        try {
            int types=booksMapper.insertSelective(books);
            returnString=types==1?ResponseMessage.LOGIN_SUCCESS:ResponseMessage.ANOTHER_ERROR;
        }catch (Exception e) {
            returnString=ResponseMessage.SERVER_ERROR;
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
        return returnString;
    }
}
